//
//  CQTagBorderStateButton.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 7/9/15.
//  Copyright (c) 2015 dvlproad. All rights reserved.
//

#import "CQTagBorderStateButton.h"

@interface CQTagBorderStateButton ()

@property (nonatomic, assign) CGFloat cjNormalBorderWidth;      /**< 设置按钮默认时候的边框宽度 */
@property (nonatomic, assign) CGFloat cjSelectedBorderWidth;    /**< 设置按钮选中时候的边框宽度 */

@property (nonatomic, strong) UIColor *cjNormalBorderColor;     /**< 设置按钮默认时候的边框颜色 */
@property (nonatomic, strong) UIColor *cjHighlightedBorderColor;/**< 设置按钮高亮时候的边框颜色 */
@property (nonatomic, strong) UIColor *cjDisabledBorderColor;   /**< 设置按钮失效时候的边框颜色 */

@property (nonatomic, strong) UIColor *cjSelectedBorderColor;   /**< 设置按钮选中时候的边框颜色 */
@property (nonatomic, strong) UIColor *cjSelectedDisabledBorderColor;   /**< 设置按钮选中且失效时候的边框颜色 */
@property (nonatomic, copy) void(^clickSubmitTitleHandle)(CQTagBorderStateButton *bButton);
@property (nonatomic, copy) void(^clickEditTitleHandle)(CQTagBorderStateButton *bButton);

@end



@implementation CQTagBorderStateButton

/*
 *  "提交"/"修改"状态选择按钮(if you want to show editTitle, you should make selected == YES)
 *
 *  @param submitTitle              submitTitle(current selected should be YES)
 *  @param editTitle                editTitle(current selected should be NO)
 *  @param showEditTitle            showEditTitle(if you want to show editTitle, you should make selected == YES)
 *  @param clickSubmitTitleHandle   click submitTitle action
 *  @param clickEditTitleHandle     click editTitle action
 */
- (instancetype)initWithSubmitTitle:(NSString *)submitTitle
                          editTitle:(NSString *)editTitle
                      showEditTitle:(BOOL)showEditTitle
             clickSubmitTitleHandle:(void(^)(UIButton *button))clickSubmitTitleHandle
               clickEditTitleHandle:(void(^)(UIButton *button))clickEditTitleHandle
{
    self = [CQTagBorderStateButton buttonWithType:UIButtonTypeCustom];
    if (self) {
        [self configThemeWithNormalTitle:submitTitle selectedTitle:editTitle];
        self.selected = showEditTitle;
        //设置的时候，就给他添加方法，省得再多个接口处理
        [self addTarget:self action:@selector(__cqtagTouchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside];
        _clickSubmitTitleHandle = clickSubmitTitleHandle;
        _clickEditTitleHandle = clickEditTitleHandle;
    }
    
    
    return self;
}

#pragma mark - Private Method
// 按钮点击
- (void)__cqtagTouchUpInsideAction:(UIButton *)button {
    if (button.selected) {
        !self.clickEditTitleHandle ?: self.clickEditTitleHandle(button);
    } else {
        !self.clickSubmitTitleHandle ?: self.clickSubmitTitleHandle(button);
    }
}



- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    
    [self __updateBorderColor];
    
    // selected 改变时候的回调
    if (self.selectedChangeCompleteBlock) {
        self.selectedChangeCompleteBlock(self);
    }
}

- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    
    [self __updateBorderColor];
}

#pragma mark - Private Method
/**
 *  根据 selected 和 enabled 更新 borderColor
 */
- (void)__updateBorderColor {
    BOOL selected = self.selected;  //是否选中
    BOOL enabled = self.enabled;    //是否可操作
    
    if (selected) {
        self.layer.borderWidth = self.cjSelectedBorderWidth;
        if (enabled) {
            if (self.cjSelectedBorderColor) {
                self.layer.borderColor = self.cjSelectedBorderColor.CGColor;
            }
        } else {
            if (self.cjSelectedDisabledBorderColor) {
                self.layer.borderColor = self.cjSelectedDisabledBorderColor.CGColor;
            }
        }
    } else {
        self.layer.borderWidth = self.cjNormalBorderWidth;
        if (enabled) {
            if (self.cjNormalBorderColor) {
                self.layer.borderColor = self.cjNormalBorderColor.CGColor;
            }
        } else {
            if (self.cjDisabledBorderColor) {
                self.layer.borderColor = self.cjDisabledBorderColor.CGColor;
            }
        }
    }
}

/*
 *  设置主题及标题
 *
 *  @param normalTitle      未选中时候的文字
 *  @param selectedTitle    选中时候的问题在
 */
- (void)configThemeWithNormalTitle:(nullable NSString *)normalTitle selectedTitle:(nullable NSString *)selectedTitle {
    UIColor *themeColor = [UIColor colorWithRed:1/255.0f green:173/255.0f blue:254/255.0f alpha:1.0];  // @"#01adfeFF");
    UIColor *themeDisabledColor = [UIColor colorWithRed:1/255.0f green:173/255.0f blue:254/255.0f alpha:0.6];  // (@"#01adfe66");
    UIColor *themeOppositeColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0];  // (@"#FFFFFF");[UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0];  // (@"#FFFFFF");
    UIColor *themeOppositeDisabledColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:0.4];  // (@"#FFFFFF4C");
    CGFloat buttonCornerRadius = 15;
    CGFloat buttonSelectedBorderWidth = 0.5;
    
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = buttonCornerRadius;
    self.cjNormalBorderWidth = 0;
    self.cjSelectedBorderWidth = buttonSelectedBorderWidth;
    
    [self setTitle:normalTitle forState:UIControlStateNormal];
    [self setTitleColor:themeOppositeColor forState:UIControlStateNormal];
//    [self setTitleColor:themeOppositeColor forState:UIControlStateNormal | UIControlStateDisabled];
//    self.cjNormalBGColor = themeColor;
    UIImage *normalBGImage = cqtag_buttonBGImage(themeColor);
    [self setBackgroundImage:normalBGImage forState:UIControlStateNormal];
    self.cjNormalBorderColor = themeColor;
//    self.cjDisabledBGColor = themeDisabledColor;
    UIImage *disabledBGImage = cqtag_buttonBGImage(themeDisabledColor);
    [self setBackgroundImage:disabledBGImage forState:UIControlStateNormal | UIControlStateDisabled];
    self.cjDisabledBorderColor = themeDisabledColor;
    
    [self setTitle:selectedTitle forState:UIControlStateSelected];
    [self setTitleColor:themeColor forState:UIControlStateSelected];
//    [self setTitleColor:themeColor forState:UIControlStateSelected | UIControlStateNormal];
    [self setTitleColor:themeDisabledColor forState:UIControlStateSelected | UIControlStateDisabled];
//    self.cjSelectedBGColor = themeOppositeColor;
    UIImage *selectedBGImage = cqtag_buttonBGImage(themeOppositeColor);
    [self setBackgroundImage:selectedBGImage forState:UIControlStateSelected];
    self.cjSelectedBorderColor = themeColor;
//    button.cjSelectedDisabledBGColor = themeOppositeDisabledColor;
    UIImage *selectedDisabledBGImage = cqtag_buttonBGImage(themeOppositeDisabledColor);
    [self setBackgroundImage:selectedDisabledBGImage forState:UIControlStateSelected | UIControlStateDisabled];
    self.cjSelectedDisabledBorderColor = themeDisabledColor;
}


/// 使用颜色构建的背景图片
UIImage *cqtag_buttonBGImage(UIColor *bgColor) {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [bgColor CGColor]);
    CGContextFillRect(context, rect);
    UIImage *normalBGImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return normalBGImage;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
